package JAVA300.第六章异常;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class TestTryCatch {
    public static void main(String[] args) {
        FileReader reader =null;
        try {
            reader = new FileReader("/Users/hqqs/Desktop/未命名.txt");//第一个catch
            System.out.println("step1");

            char c1 = (char)reader.read();//第二个catch
            System.out.println(c1);
        }catch (FileNotFoundException e){//该异常为下一个IO异常的子类，子类在前
            System.out.println("step2");

            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {//reader读取完之后需要关闭,读取不了try中变量

            try {
                if (reader!=null){
                    System.out.println("step3");
                    reader.close();//也需要捕获异常
                }

            }catch (IOException e){
                e.printStackTrace();
            }

        }


    }
}
